package guard

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Guard interface {
	Guard(c *gin.Context) (interface{}, error)
}

func ParamValidator(g Guard) gin.HandlerFunc {
	return func(c *gin.Context) {

		var (
			param interface{}
			err   error
		)

		if param, err = g.Guard(c); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  err.Error(),
			})
			c.Abort()
			return
		}

		c.Set("param", param)
		c.Next()
	}
}

func Pv(g Guard) gin.HandlerFunc {
	return ParamValidator(g)
}